package com.aat.dxfy.redis.test;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class RedisUse {

	public static void main(String[] args) throws Exception {
		JedisPoolConfig config = new JedisPoolConfig();
		config.setMaxIdle(5);
		config.setMaxWaitMillis(100L);
		config.setTestOnBorrow(false);

		JedisPool jedisPool = new JedisPool(config, "127.0.0.1", 6379, 0, "redis");
		Jedis jedis = jedisPool.getResource();
		String key = "1";
		int seconds = 5;

		jedis.set(key, "1");
		jedis.expire(key, seconds);
		System.out.println(jedis.get(key));

		Thread.sleep(3000);

		if (jedis.exists(key)) {
			String s = jedis.get(key);
			Integer i1 = Integer.parseInt(s) + 1;
			jedis.set(key, "" + i1);
			jedis.expire(key, seconds);
		}

		System.out.println(jedis.get(key));

		Thread.sleep(3000);

		if (jedis.exists(key)) {
			String s = jedis.get(key);
			Integer i1 = Integer.parseInt(s) + 1;
			jedis.set(key, "" + i1);
			jedis.expire(key, seconds);
		}

		System.out.println(jedis.get(key));

		Thread.sleep(5000);

		if (jedis.exists(key)) {
			String s = jedis.get(key);
			Integer i1 = Integer.parseInt(s) + 1;
			jedis.set(key, "" + i1);
			jedis.expire(key, seconds);
		}

		System.out.println(jedis.get(key));

		Thread.sleep(6000);

		if (jedis.exists(key)) {
			String s = jedis.get(key);
			Integer i1 = Integer.parseInt(s) + 1;
			jedis.set(key, "" + i1);
			jedis.expire(key, seconds);
		}

		System.out.println(jedis.get(key));

		jedis.close();
		jedisPool.destroy();
	}

}
